"""Peewee migrations: ::

    > Model = migrator.orm['name']

    > migrator.sql(sql)
    > migrator.python(func, *args, **kwargs)
    > migrator.create_model(Model)
    > migrator.remove_model(Model, cascade=True)
    > migrator.add_fields(Model, **fields)
    > migrator.change_fields(Model, **fields)
    > migrator.remove_fields(Model, *field_names, cascade=True)
    > migrator.rename_field(Model, old_field_name, new_field_name)
    > migrator.rename_table(Model, new_table_name)
    > migrator.add_index(Model, *col_names, unique=False)
    > migrator.drop_index(Model, index_name)
    > migrator.add_not_null(Model, field_name)
    > migrator.drop_not_null(Model, field_name)
    > migrator.add_default(Model, field_name, default)

"""

import datetime as dt
import peewee as pw


def migrate(migrator, database, fake=False, **kwargs):
    """Write your migrations here."""

    migrator.remove_fields('order', 'review_of_seller', 'review_of_buyer')

    migrator.change_fields('order',
                           amount=pw.DecimalField(auto_round=False, decimal_places=4, default=0.0, max_digits=10,
                                                  rounding='ROUND_HALF_EVEN'),
                           discount_amount=pw.DecimalField(auto_round=False, decimal_places=4, default=0.0,
                                                           max_digits=10, rounding='ROUND_HALF_EVEN'),
                           real_fee=pw.DecimalField(auto_round=False, decimal_places=4, default=0.0, max_digits=10,
                                                    rounding='ROUND_HALF_EVEN'),
                           real_amount=pw.DecimalField(auto_round=False, decimal_places=4, default=0.0, max_digits=10,
                                                       rounding='ROUND_HALF_EVEN'),
                           fee=pw.DecimalField(auto_round=False, decimal_places=4, default=0.0, max_digits=10,
                                               rounding='ROUND_HALF_EVEN'))

    migrator.add_fields(
        'accounthistory',

        created_at=pw.DateTimeField(default=dt.datetime.now),
        updated_at=pw.DateTimeField(default=dt.datetime.now),
        settle_time=pw.DateTimeField(null=True),
        order=pw.ForeignKeyField(db_column='order_id', null=True,
                                 rel_model=migrator.orm['order'], to_field='id'))

    migrator.change_fields('orderdetail',
                           amount=pw.DecimalField(auto_round=False, decimal_places=4, default=0.0, max_digits=10,
                                                  rounding='ROUND_HALF_EVEN'),
                           discount_amount=pw.DecimalField(auto_round=False, decimal_places=4, default=0.0,
                                                           max_digits=10, rounding='ROUND_HALF_EVEN'),
                           real_amount=pw.DecimalField(auto_round=False, decimal_places=4, default=0.0, max_digits=10,
                                                       rounding='ROUND_HALF_EVEN'))


def rollback(migrator, database, fake=False, **kwargs):
    """Write your rollback migrations here."""

    migrator.change_fields('ticket',
                           created_at=pw.DateTimeField(default=datetime.datetime(2016, 11, 8, 21, 23, 10, 324587)),
                           updated_at=pw.DateTimeField(default=datetime.datetime(2016, 11, 8, 21, 23, 10, 324607)))

    migrator.change_fields('inventory',
                           created_at=pw.DateTimeField(default=datetime.datetime(2016, 11, 8, 21, 23, 10, 324125)),
                           updated_at=pw.DateTimeField(default=datetime.datetime(2016, 11, 8, 21, 23, 10, 324147)))

    migrator.change_fields('orderdetail',
                           amount=pw.DecimalField(auto_round=False, decimal_places=2, default=0.0, max_digits=10,
                                                  rounding='ROUND_HALF_EVEN'),
                           discount_amount=pw.DecimalField(auto_round=False, decimal_places=2, default=0.0,
                                                           max_digits=10, rounding='ROUND_HALF_EVEN'),
                           real_amount=pw.DecimalField(auto_round=False, decimal_places=2, default=0.0, max_digits=10,
                                                       rounding='ROUND_HALF_EVEN'))

    migrator.add_fields(
        'order',

        review_of_seller=pw.CharField(default='N', max_length=1),
        review_of_buyer=pw.CharField(default='N', max_length=1))

    migrator.change_fields('order',
                           amount=pw.DecimalField(auto_round=False, decimal_places=2, default=0.0, max_digits=10,
                                                  rounding='ROUND_HALF_EVEN'),
                           discount_amount=pw.DecimalField(auto_round=False, decimal_places=2, default=0.0,
                                                           max_digits=10, rounding='ROUND_HALF_EVEN'),
                           real_fee=pw.DecimalField(auto_round=False, decimal_places=2, default=0.0, max_digits=10,
                                                    rounding='ROUND_HALF_EVEN'),
                           real_amount=pw.DecimalField(auto_round=False, decimal_places=2, default=0.0, max_digits=10,
                                                       rounding='ROUND_HALF_EVEN'),
                           fee=pw.DecimalField(auto_round=False, decimal_places=2, default=0.0, max_digits=10,
                                               rounding='ROUND_HALF_EVEN'))

    migrator.remove_fields('accounthistory', 'created_at', 'updated_at', 'settle_time', 'order')
